//
//  IWillHelper.m
//  BrushFace
//
//  Created by timer_q on 15/12/11.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "IWillHelper.h"

@implementation IWillHelper
DEF_SINGLETON(IWillHelper)

+ (void)helperChooseMoneyButtonClickWithIndex:(int)index fromDict:(NSDictionary *)dict{
    NSArray *labelArray = dict[@"label"];
    NSArray *imageArray = dict[@"img"];
    NSArray *selectImageArray = dict[@"selectImg"];
    for (int i=0; i<labelArray.count; i++) {
        UILabel *label = labelArray[i];
        UIImageView *imageView = imageArray[i];
        UIImageView *selectImg = selectImageArray[i];
        if (i==index) {
            label.textColor = [UIColor darkGrayColor];
            [IWillHelper sharedInstance].amount = label.text;
            imageView.highlighted = YES;
            selectImg.hidden = NO;
        }else{
            label.textColor = [UIColor lightGrayColor];
            imageView.highlighted = NO;
            selectImg.hidden = YES;
        }
        
    }
}
+ (void)helperReasonButtonSelectButton:(UIButton *)sender withButtonArray:(NSArray *)buttonArr{
    for (UIButton *button in buttonArr) {
        if ([sender isEqual:button]) {
            button.selected = YES;
        }else{
            button.selected = NO;
        }
    }
}
+(NSString *)fetchMoney{
    return [self fetchMoneyWithLixi:nil];
}
#pragma mark------qiweipeng
//这个方法整个替换一下～替换完后麻烦运行看看，我要借款－三个月，日利率0.05%，推荐利息是30.17，是的话就对啦

+ (NSString *)fetchMoneyWithLixi:(NSString *)lixiStr{
    
    int priod = [[IWillHelper sharedInstance].priod intValue];
    
    if (lixiStr==nil) {
        lixiStr = [IWillHelper sharedInstance].couponsid;
    }
    if ([lixiStr integerValue]==0) {
        return @"0.00";
    }
    
    float lilv = [lixiStr floatValue]/10000.0;
    float amount = [[IWillHelper sharedInstance].amount floatValue];
    CGFloat mouthrate = lilv*30;
    //月本金之和
    float monthcapitalcount = 0;
    //月利息之和
    float monthinterestcount = 0;
    for(float i=0;i<priod;i++) {
        //每月还款本金
        float monthcapital = 0;
        //每月还款利息
        float monthinterest = 0;
        //每个月的还款总额（利息跟本金）
        float  monthamount = (amount * mouthrate * (pow((1 + mouthrate), priod)))
        / (pow((1 + mouthrate), priod) - 1);
        if(i == priod-1) {
            //最后一期还款本金=借款本金-前几期还款本金总和
            
            monthcapital = [self decimalwithfloatV:(amount - monthcapitalcount)];
            monthinterest = [self decimalwithfloatV:(monthamount - monthcapital)];
            monthinterestcount = monthinterestcount + monthinterest*100;
        }
        else {
            monthcapital = [self decimalwithfloatV:(amount * mouthrate * (pow((1 + mouthrate), i))
                                                    / (pow((1 + mouthrate), priod) - 1))];
            monthinterest = [self decimalwithfloatV:(monthamount - monthcapital)];
            monthcapitalcount = monthcapitalcount + monthcapital;
            monthinterestcount = monthinterestcount + monthinterest*100;
        }
    }
    
    return [NSString stringWithFormat:@"%.2f",monthinterestcount/100];
}


+ (float) decimalwithfloatV:(float)floatV
{
    extern float ceilf(float);
    
    extern double ceil (double);
    
    int result = (int)ceilf(floatV *100);
    
    return (float)result/100;
    
}

#pragma mark---------------------------
+ (NSString *)notRounding:(float)price afterPoint:(int)position{
    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundUp scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    NSDecimalNumber *ouncesDecimal;
    NSDecimalNumber *roundedOunces;
    
    ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];
    roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
    return [NSString stringWithFormat:@"%@",roundedOunces];
}
@end
